package com.lzk.product.service.imp;


import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.BaiduAuditUtils;
import com.lzk.basic.util.JsonResult;
import com.lzk.product.domain.Product;
import com.lzk.product.domain.ProductDetail;
import com.lzk.product.mapper.ProductDetailMapper;
import com.lzk.product.mapper.ProductMapper;
import com.lzk.product.service.IProductService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;

    @Transactional
    @Override
    public void save(Product product) {
        //对name、resources进行审核
        Boolean nameBoo = BaiduAuditUtils.TextCensor(product.getName());
        if(!nameBoo){
            throw new BusinessException("内容违规，请整改！");
        }
        String resources = product.getResources();
        String[] urls = resources.split(",");
        for (String url : urls) {
            Boolean imgBoo = BaiduAuditUtils.ImgCensor("http://123.207.27.208"+url);
            if(!imgBoo){
                throw new BusinessException("图片违规，请整改！");
            }
        }

        super.save(product);
        //获取宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProduct_id(product.getId());
            //添加宠物详情
            productDetailMapper.save(productDetail);
        }
    }

    @Transactional
    @Override
    public void update(Product product) {
        //对name、resources进行审核
        Boolean nameBoo = BaiduAuditUtils.TextCensor(product.getName());
        if(!nameBoo){
            throw new BusinessException("内容违规，请整改！");
        }
        String resources = product.getResources();
        String[] urls = resources.split(",");
        for (String url : urls) {
            Boolean imgBoo = BaiduAuditUtils.ImgCensor("http://123.207.27.208"+url);
            if(!imgBoo){
                throw new BusinessException("图片违规，请整改！");
            }
        }

        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            //之前在分页查询已经将id查询
            productDetailMapper.update(productDetail);
        }
    }

    @Transactional
    @Override
    public void remove(Long id) {
        productDetailMapper.removeProductDetailByProductId(id);
        super.remove(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchRemove(List<Long> ids) {
        productDetailMapper.removeProductDetailByProductIds(ids);
        super.patchRemove(ids);
    }

    /**
     * 下架业务实现
     * @param ids
     * @return
     */
    @Override
    public JsonResult offSale(List<Long> ids) {
        Date date = new Date();
        productMapper.offSale(ids,date);
        return JsonResult.me();
    }

    /**
     * 上架业务实现
     * @param ids
     * @return
     */
    @Override
    public JsonResult onSale(List<Long> ids) {
        Date date = new Date();
        productMapper.onSale(ids,date);
        return JsonResult.me();
    }


}
